from sqlalchemy import Column, String, Integer, Text, ForeignKey
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import relationship

from app.core.database import Base


class UploadedFile(Base):
    __tablename__ = "uploaded_files"

    user_id = Column(UUID(as_uuid=True), ForeignKey("users.id"), nullable=True)
    filename = Column(String, nullable=False)
    file_path = Column(String, nullable=False, unique=True)
    content_type = Column(String, nullable=False)
    file_size = Column(Integer, nullable=False)
    file_type = Column(
        Integer,
        nullable=False,
        comment="文件类型: 1=医疗档案, 2=普通文件, 3=执业资质材料",
    )
    parsed_content = Column(Text, nullable=True)

    owner = relationship("User")
    messages = relationship("Message", secondary="message_files", back_populates="files")
    medical_record = relationship("MedicalRecord", back_populates="file", uselist=False)
    certificate_bindings = relationship(
        "DoctorCertificateFile",
        back_populates="file",
        cascade="all, delete-orphan",
    )
